package com.mbc.rmt.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.mbc.common.dao.DAO;
import com.mbc.common.param.CriteriaParams;
import com.mbc.rmt.data.COACurrencyDataList;
import com.mbc.rmt.entities.COACurrency;

public class IAIMSCOACurrencyDAOImpls extends DAO<COACurrency> implements IAIMSCOACurrencyDAO {
	
	public static final String field_coacurrencykey = "COACurrencyKey";
	public static final String field_coakey = "COAKey";
	public static final String field_currencykey = "CurrencyKey";
	public static final String field_departmentkey = "DepartmentKey";
	public static final String field_srno = "SrNo";
	public static final String field_createddate = "CreatedDate";
	public static final String field_updateddate = "UpdatedDate";
	public static final String field_createduser = "CreatedUser";
	public static final String field_updateduser = "UpdatedUser";
	public static final String field_office_id = "Office_Id";
	
	public IAIMSCOACurrencyDAOImpls(Connection conn) {
		this.conn = conn;
		this.table_name = "COACurrency";
	}

	@Override
	public void create(COACurrency entity) throws SQLException {
		query = insertParam(table_name, new String[] { field_coacurrencykey, field_coakey, field_currencykey,
				field_departmentkey, field_srno, field_createddate, field_createduser, field_office_id });
		pstmt = conn.prepareStatement(query);
		updateRecord(entity);
		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	public void update(COACurrency entity) throws SQLException {
		query = updateParam(table_name, new String[] { field_coacurrencykey, field_coakey, field_currencykey,
				field_departmentkey, field_srno, field_updateddate, field_updateduser, field_office_id }) + " AND " + field_coacurrencykey + " = " + entity.getCoacurrencykey();
		pstmt = conn.prepareStatement(query);
		updateRecord(entity);
		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	public void delete(long coakey) throws SQLException {
		query = getDeleteQuery() + " WHERE " + field_coakey + " = " + coakey;
		pstmt = conn.prepareStatement(query);
		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	public COACurrency find(long key) throws SQLException {
		COACurrency l_COACurrency = null;
		query = getSelectQuery() + " WHERE " + field_coacurrencykey + " = " + key;
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		if (rs.next())
			l_COACurrency = readRecord(rs);
		rs.close();
		pstmt.close();
		return l_COACurrency;
	}

	@Override
	public List<COACurrency> findAll() throws SQLException {
		ArrayList<COACurrency> l_COACurrencyList = new ArrayList<COACurrency>();
		query = getSelectQuery();
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		while (rs.next()) {
			l_COACurrencyList.add(readRecord(rs));
		}
		rs.close();
		pstmt.close();
		return l_COACurrencyList;
	}

	@Override
	public COACurrencyDataList findCOACurrencys(CriteriaParams cri)
			throws SQLException {
		COACurrencyDataList l_DataList = new COACurrencyDataList();
		ArrayList<COACurrency> l_entityList = new ArrayList<COACurrency>();
		String l_cri = "";
		query = "set rowcount "
				+ cri.getSize()
				+ " select * from (select row_number() over(order by coacurrencykey) as [rowno], * from "
				+ table_name + " where 1 = 1" + l_cri + ") " + table_name
				+ " where rowno > " + cri.getIndex();
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		if (rs.next()) {
			do {
				l_entityList.add(readRecord(rs));
			} while (rs.next());
		}
		l_DataList.setEntityList(l_entityList);
		rs.close();
		pstmt.close();
		query = "select count(coacurrencykey) from " + table_name
				+ " where 1 = 1" + l_cri;
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		if (rs.next()) {
			l_DataList.setTotalRecord(rs.getLong(1));
		}
		rs.close();
		pstmt.close();
		return l_DataList;
	}

	@Override
	protected COACurrency readRecord(ResultSet rs) throws SQLException {
		COACurrency l_entity = new COACurrency();
		l_entity.setCoacurrencykey(rs.getLong("coacurrencykey"));
		l_entity.setCoakey(rs.getLong("coakey"));
		l_entity.setCurrencykey(rs.getLong("currencykey"));
		l_entity.setDepartmentkey(rs.getLong("departmentkey"));
		return l_entity;
	}
	
	protected void updateRecord(COACurrency entity) throws SQLException {
		int i = 1;
		pstmt.setLong(i++, entity.getCoacurrencykey());
		pstmt.setLong(i++, entity.getCoakey());
		pstmt.setLong(i++, entity.getCurrencykey());
		pstmt.setLong(i++, entity.getDepartmentkey());
		pstmt.setString(i++, null);
		pstmt.setString(i++, null);
		pstmt.setString(i++, null);
		pstmt.setString(i++, null);
	}

}
